import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { BusinessNoticeEditComponent } from './edit/edit.component';
@Component({
  selector: 'business-notice',
  templateUrl: './notice.component.html',
})
export class BusinessNoticeComponent implements OnInit {
  queryStr: any = {
    offset: 0,
    limit: 10
  };
  loading = false;
  data: any[] = [];
  searchSchema: SFSchema = {
    properties: {
      no: {
        type: 'string',
        title: '标题'
      }
    }
  };
  @ViewChild('st') st: STComponent;
  columns: STColumn[] = [
    { title: '标题', index: 'title' },
    { title: '内容', index: 'describe' },
    // { title: '时间', type: 'date', index: 'updatedAt' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: (item: any) => this.openEdit(item)
        },
        {
          text: '删除',
          click: (item: any) => this.remove(item._id)
        },
      ],
    },
  ];
  constructor(
    private http: _HttpClient,
    private modalSrv: NzModalService,
    public msg: NzMessageService,
    private modal: ModalHelper
  ) { }
  ngOnInit() {
    this.getData();
  }
  getData() {
    this.loading = true;
    this.http.get('api/notice/list', this.queryStr).subscribe((res: any) => {
      this.data = res.objects;
      this.loading = false;
    });
  }
  // 删除
  remove(id) {
    this.modalSrv.confirm({
      nzTitle     : '是否确认删除?',
      nzContent   : '<b style="color: red;">删除后不可恢复</b>',
      nzOkText    : '是',
      nzOkType    : 'danger',
      nzOnOk      : () => {
        this.http.delete('api/notice/delete/', {id}).subscribe((res: any) => {
          if(res.success) {
            this.msg.success(`删除成功`);
            this.getData();
            this.st.clearCheck();
          }else{
            this.msg.error(`删除失败`);
          }
        }, () => {
          this.msg.error(`网络或服务器错误`);
        });
      },
      nzCancelText: '否',
      nzOnCancel  : () => console.log('Cancel')
    });
  }
  // 编辑新增
  openEdit(i: any = {}) {
    this.modal.create(BusinessNoticeEditComponent, { i }, { size: 'md' })
      .subscribe(res => {
        if(i._id) {
          res['_id'] = i._id;
          this.http.patch('api/notice/update/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`修改成功`);
              this.getData();
            }else{
              this.msg.error(`公告已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }else {
          this.http.post('api/notice/add/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`添加成功`);
              this.getData();
            }else{
              this.msg.error(`公告已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }
      });
  }
}
